package com.zhida.jiyangcenter.account

import android.os.Build
import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import androidx.lifecycle.lifecycleScope
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.manager.app
import chooongg.base.utils.*
import chooongg.http.APIException
import chooongg.http.HttpCallback
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APIAccount
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.NetResponse
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.mian.MainActivity
import com.zhida.jiyangcenter.utils.REGEX_PASSWORD
import com.zhida.jiyangcenter.utils.SP
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.activity_forget_pass.*


@TitleBar(false)
@ContentLayout(R.layout.activity_forget_pass)
class ForgetPassActivity : BaseActivity() {
    override fun initConfig(savedInstanceState: Bundle?) {
        val window = activity.window
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        title_bar.navigationIcon(R.drawable.iv_back_fill) {
            finish()
        }
        switch_password.setOnClickListener {
            if (edit_password.transformationMethod == PasswordTransformationMethod.getInstance()) {
                switch_password.setImageResource(R.drawable.iv_login_visible)
                edit_password.transformationMethod = HideReturnsTransformationMethod.getInstance()
            } else {
                switch_password.setImageResource(R.drawable.iv_login_invisible)
                edit_password.transformationMethod = PasswordTransformationMethod.getInstance()
            }
        }
        btn_login.setOnClickListener {
            when {
                edit_password.text.toString()
                    .isEmpty() -> showSoftInput(edit_password).apply { tipWarn(R.string.login_password_empty) }
                edit_password.text.toString().length < 8 -> showSoftInput(edit_password).apply {
                    tipWarn(
                        "密码最少位8位"
                    )
                }
                !edit_password.text.toString()
                    .regex(REGEX_PASSWORD) -> showSoftInput(edit_password).apply { tipWarn(R.string.login_password_wrong) }
                else -> {
                    lifecycleScope.request({
                        APIAccount.service().passwordForget(
                            edit_password.text.toString(), edit_password.text.toString()
                        )
                    }, object : BaseAPI<Any>() {
                        override fun onError(e: APIException) {
                            tipFail(e.eMessage)
                        }

                        override fun onSuccess(t: Any?, state: Int, msg: String) {
                            tipSuccess(msg)
                            startActivity<LoginPassActivity> { }
                            app.activityStack.forEach {
                                if (it is MainActivity || it is LoginPassActivity) {

                                }else{
                                    it.finish()
                                }
                            }
                            finish()
                        }

                    })

                }
            }
        }
    }
}